//
// Created by Huafv on 2024/10/10.
//

#include <stdint.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>

#include <termios.h>
/*************************** Defines *****************************/
#define PHY_BUS_IF_NAME "/dev/ttyUSB0"


/*************************** vars *****************************/
int UART_FD = -1;
struct termios termios_options;

/*************************** Functions prototype *****************************/
static void UART_BT_Open(void);
static void UART_BT_Close(void);
static void UART_BT_Transmit(uint8_t *pData, uint16_t Size);
static int UART_BT_Receive(uint8_t *pData, uint16_t Size);


/*************************** Functions impl *****************************/
void bt_uart_test()
{
    uint8_t hci_reset[4] = {0x01,0x03,0x0c,0x00};
    UART_BT_Transmit(hci_reset,4);
}


int main(){
    uint8_t  receive_buffer[128] = {0};
    int i = 0;
    UART_BT_Open();

    i = 10;
    while (i--)
    {
        bt_uart_test();
        sleep(1);
    }

    int len = UART_BT_Receive(receive_buffer, sizeof(receive_buffer));
    printf("Receive data len : %d\r\n", len);

    for (int j = 0; j < len; ++j) {
        printf("0x%02x ", receive_buffer[j]);
        if( (j % 10) == 0){
            fflush(stdout);
        }
    }

    /* close src or while */
    while(1)
    {
        sleep(1);
    }
    UART_BT_Close();
    return 0;
}

/**
 * @brief Transport layer Type H4 open
 */
static void UART_BT_Open(void){
    int ret = 0;

    int flags = O_RDWR | O_NOCTTY;
    UART_FD = open(PHY_BUS_IF_NAME, flags);
    if(UART_FD < 0){
        printf("uart open fail\r\n");
    }else{
        printf("uart open success\r\n");
    }

    /* UART arguments set */
    ret = tcgetattr(UART_FD, &termios_options);
    if(ret < 0){
        printf("tcgetattr fail\r\n");
    }else{
        printf("tcgetattr success\r\n");
    }

    cfmakeraw(&termios_options);

    // 8N1
    termios_options.c_cflag &= ~CSTOPB;
    termios_options.c_cflag |= CS8;

    termios_options.c_cflag |= CREAD | CLOCAL | CRTSCTS;
    termios_options.c_iflag &= ~(IXON | IXOFF | IXANY);
    termios_options.c_cflag &= ~PARENB;

    termios_options.c_cc[VMIN]  = 1;
    termios_options.c_cc[VTIME] = 0;

    if(tcsetattr(UART_FD, TCSANOW, &termios_options) < 0)
    {
        printf("ERROR:Couldn't set term attributes\n");
    }

    if (tcgetattr(UART_FD, &termios_options) < 0)
    {
        printf("ERROR:Couldn't get term attributes\n");
    }

    cfsetospeed(&termios_options, B115200);
    cfsetispeed(&termios_options, B115200);


    if( tcsetattr(UART_FD, TCSANOW, &termios_options) < 0)
    {
        printf("ERROR:Couldn't set term attributes\n");
    }
}

/**
 * @brief Transport layer Type H4 close
 */
static void UART_BT_Close(void){
    if(UART_FD > 0)
    {
        close(UART_FD);
    }
}

static void UART_BT_Transmit(uint8_t *pData, uint16_t Size){
    write(UART_FD, pData, Size);
}

static int UART_BT_Receive(uint8_t *pData, uint16_t Size){
    return read(UART_FD, pData, Size);
}